package yxy.game.pm2.command.union.leader;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionLeaderMessage;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.player.UnionPlayer;

/** 联盟盟主 联盟申请同意或拒绝 */
@MessageType(proto = Command.UnionApplyDeal, classtype = UnionLeaderMessage.UnionApplyDealRequest.class)
public class CMD_UnionApplyDeal extends Command<UnionLeaderMessage.UnionApplyDealRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionLeaderMessage.UnionApplyDealRequest request) {
		String playerCode = messageSource.getTag();
		String code = request.getCode();
		boolean confirm = request.getConfirm();

		UnionLeaderMessage.UnionApplyDealResponse.Builder response = UnionLeaderMessage.UnionApplyDealResponse.newBuilder();

		// 管理员权限
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = new UnionHandle().checkPlayerUnionManager(entry);
		if (!result.equals(Result.UnionManager)) {
			send(messageSource, response, result);
			return;
		}

		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		try {
			if (uLock.tryLock()) {
				UnionBean union = entry.reloadUnionBean();

				response.setCode(code);
				response.setConfirm(confirm);

				// 不同意
				if (!confirm) {
					union.getApplyList().remove(code);
					union.set();
					send(messageSource, response, Result.Success);
					return;
				}

				// 同意
				RedisLock pLock = UnionPlayer.lock(code);
				try {
					if (pLock.tryLock()) {

						UnionPlayerEntry applyentry = new UnionPlayerEntry(code);
						result = new UnionHandle().checkPlayerUnion(applyentry);
						if (!result.equals(Result.UnionOut)) {
							// 申请者不能加入联盟
							union.getApplyList().remove(code);
							union.set();
							send(messageSource, response, Result.UnionApplierIsIn);// 申请者已经有联盟了
							return;
						}

						UnionPlayer applyPlayer = applyentry.getUnionPlayer();

						if (!union.getApplyList().contains(code)) {
							send(messageSource, response, Result.ParamError);
							return;
						}

						result = new UnionHandle().addMember(union, applyPlayer);
						if (result.equals(Result.Success)) {
							union.getApplyList().remove(code);
							union.set();
						}
						send(messageSource, response, result);

					}
				} catch (Exception e) {
					e.printStackTrace();
					send(messageSource, response, Result.DataError);
				} finally {
					pLock.unlock();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionLeaderMessage.UnionApplyDealResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}